/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui.handlers;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

/**
 *
 * @author mazzahaka
 */
public class PropertiesHandler {
  private HashMap prop ;
  private FileOutputStream fo;
  private ObjectOutputStream oo;
  private ObjectInputStream oi;
  private FileInputStream fi;
  private String ConfigsList;
        public PropertiesHandler() throws IOException, ClassNotFoundException{
        prop = LoadMap();
       
        
       
        LogHandler.info("PropertiesHandler Daemon loaded!");
    }
        private HashMap LoadMap (){
            HashMap map=null;
          try {
             fi = new FileInputStream("config.properties");
          } catch (FileNotFoundException ex) {
             LogHandler.err("File not found!");
            }
          try {
             oi = new ObjectInputStream(fi);
          } catch (IOException ex) {
             LogHandler.err("Internal Error!");
          }
          try {
             map = (HashMap) oi.readObject();
          } catch (IOException ex) {
             LogHandler.err("Internal Error!");
          } catch (ClassNotFoundException ex) {
             LogHandler.err("Internal Error!");
          }
          return map;
     } //method for load HashMap of config file!
        private void SaveMap(HashMap map){
        try {
            fo = new FileOutputStream("config.properties");
        } catch (FileNotFoundException ex) {
           LogHandler.err("File not found!");
        }
        try {
            oo = new ObjectOutputStream(fo);
        } catch (IOException ex) {
            LogHandler.err("Internal Error!");
        }
        try {
            oo.writeObject(map);
        } catch (IOException ex) {
             LogHandler.err("Internal Error!");
        }
      } //method for save HashMap in config file!
        private void AddToMap(Object key,Object Value){prop.put(key, Value);SaveMap(prop);} // method for Add value to HashMap
        public Object ReadOfMap(Object key){ return prop.get(key);}// method for read value of HashMap
        private void ClearMap(){prop.clear(); SaveMap(prop);}//method for clear all values in HashMap
          
}
